<template>
    <!-- 搜索框 -->
    <el-form :inline="true" :model="queryInfo" class="demo-form-inline">

        <el-form-item label="文章类型">
            <el-input v-model="queryInfo.name" placeholder="请输入类型名" clearable />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="getList">搜索</el-button>
        </el-form-item>
        <div>
            <el-button @click="toAdd" type="primary" icon="Plus" plain>新增</el-button>
        </div>
    </el-form>
    <!-- 信息展览 -->
    <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="id" width="50" />

        <el-table-column prop="name" label="文章标题" width="120" />

        <el-table-column fixed="right" label="操作" min-width="100">
            <template #default="scope">
                <el-button link type="primary" size="small" @click="toEdit(scope.row)">修改</el-button>
                <el-button link type="primary" @click="onDel(scope.row.id)" size="small">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination style="margin-top: 10px;" v-model:current-page="queryInfo.pageNum"
        v-model:page-size="queryInfo.pageSize" :page-sizes="[1, 5, 10, 20]" size="default" background="false"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    <!-- 添加编辑弹框 -->
    <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500">
        <el-form :model="articleType">
            <el-form-item label="文章标题">
                <el-input v-model="articleType.name" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="saveOrUpdate">
                    {{ saveOrUpdateTitle }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus'

// 修改或添加
const saveOrUpdateVisible = ref(false)
const articleType = ref({
    id: '',
    name: ''
})
const saveOrUpdateTitle = ref('添加')
// 去修改的界面
const toEdit = row => {
    saveOrUpdateTitle.value = '修改'
    saveOrUpdateVisible.value = true
    articleType.value = row
}
// 去添加的界面
const toAdd = row => {
    saveOrUpdateTitle.value = '添加'
    saveOrUpdateVisible.value = true
    articleType.value = {
        id: '',
        name: ''
    }
}
// 取消修改或保存
const close = () => {
    saveOrUpdateVisible.value = false
    // 清空表单
    articleType.value = {
        id: '',
        name: ''
    }
}
// 保存或修改
const saveOrUpdate = () => {
    if (saveOrUpdateTitle.value == '添加') {
        // 检查是否存在相同名称的文章类型
        const existingType = tableData.value.find(type => type.name === articleType.value.name);
        if (existingType) {
            ElMessage.error('文章类型已存在，添加失败');
            return;
        }
        //调用后端接口添加
        axios.post('/articleType', articleType.value)
            .then((resp) => {
                let result = resp.data
                if (result.code == 1) {
                    // 重新获取列表
                    getList()
                    ElMessage.success('添加文章成功')
                    saveOrUpdateVisible.value = false
                } else {
                    ElMessage.error('添加文章失败')
                }
            })
    } else {
        //调用后端接口修改
        axios.put('/articleType', articleType.value)
            .then(resp => {
                let result = resp.data
                if (result.code == 1) {
                    // 重新获取列表
                    getList()
                    ElMessage.success('修改文章成功')
                    saveOrUpdateVisible.value = false
                } else {
                    ElMessage.error('修改文章失败')
                }
            })
    }
    // 关闭弹框
    saveOrUpdateVisible.value = false
    // 请求图片展示
    tabularChart.value = ''
    // 清空表单
    articleType.value = {
        id: '0',
        name: ''
    }
}


const tableData = ref([])
//   定义一个方法当页面加载时就调用
onMounted(() => {
    getList()
})
//   定义一个全局参数变量
const queryInfo = ref({
    pageNum: 1,
    pageSize: 5,
    id: '0',
    name: ''
})
// 定义一个数据总条数
const total = ref(0)
//   定义一个方法，用于获取文章列表
const getList = () => {
    // 使用axios调用后端接口获取数据
    axios.get('/articleType/selectAll', {
        params: queryInfo.value
    })
        .then((resp) => {
            let result = resp.data // 后端返回的数据
            if (result.code == 1) {
                total.value = result.data.total
                tableData.value = result.data.list
            } else {
                ElMessage.error('获取文章列表失败')
            }
        })
}
// 分页
const handleSizeChange = (val) => {
    queryInfo.value.pageSize = val;
    queryInfo.value.pageNum = 1;
    getList();
}
const handleCurrentChange = (val) => {
    queryInfo.value.pageNum = val;
    getList();
}

// 删除
const onDel = id => {
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            //调用后端接口删除
            axios.delete('/articleType/' + id)
                .then((resp) => {
                    let result = resp.data
                    if (result.code == 1) {
                        // 重新获取列表
                        getList()
                        ElMessage.success('删除成功')
                        getList()
                    } else {
                        ElMessage.error('删除失败')
                    }
                })
        })
        .catch(() => { })
}

</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed black;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
    border: 1px solid black;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 220px;
}
</style>